package com.weipeep.demo;

import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.borders.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.UnitValue;

public class PageFooterEventHandler implements IEventHandler {
    private PdfFont pdfFont;
    private int totalPages;
    public PageFooterEventHandler(PdfFont pdfFont, int totalPages) {
        this.pdfFont = pdfFont;
        this.totalPages = totalPages;
    }
    @Override
    public void handleEvent(Event event) {

        PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
        PdfPage pdfPage = docEvent.getPage();
        PdfDocument pdfDoc = docEvent.getDocument();
        int pageNumber = pdfDoc.getPageNumber(pdfPage);
        PdfCanvas pdfCanvas = new PdfCanvas(pdfPage);
        Rectangle pageSize = docEvent.getPage().getPageSize();
        Canvas canvas = new Canvas(pdfCanvas,pdfDoc,pageSize);
        canvas.add(new Paragraph("第"+pageNumber+"页，共"+totalPages+"页").setTextAlignment(TextAlignment.RIGHT).setFont(pdfFont).setFontSize(8).setFixedPosition(400,12,100));
        canvas.close();
        pdfCanvas.release();
    }

}